using System;
using System.Collections;
using System.Collections.Generic;
using cfg.level;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.U2D;

namespace QFramework.Monk
{
    public class ExpUpSystem : AbstractSystem
    {
        private ConfigModel configModel;
        public EasyEvent<SkillItem> expUpEvent = new EasyEvent<SkillItem>();
        public List<ExpUpItem> expUpItems = new List<ExpUpItem>();
        protected override void OnInit()
        {
            configModel= this.GetModel<ConfigModel>();
        }
        

        public void RefeshExpUpItems()
        {
            expUpItems.Clear();
            var levelUpConfig = configModel.GetLevelUpConfig(AppStart.level.Value);
            var list = levelUpConfig.List;

            //从list中随机三个不重复的能力，且避免出现满级的能力
            var tempList = new List<SkillType>(list); // 创建一个副本避免修改原始列表
            for (int i = 0; i < 3; i++)
            {
                if (tempList.Count == 0) break; // 防止空引用错误
                int index = UnityEngine.Random.Range(0, tempList.Count);
                var randomSkillType = tempList[index];
                var skillItem = RandomSkillItem(randomSkillType);
                if (skillItem==null)
                {
                    //重新随机一个
                    index = UnityEngine.Random.Range(0, tempList.Count);
                    randomSkillType = tempList[index];
                    skillItem = RandomSkillItem(randomSkillType);
                }
                if (skillItem != null)
                {
                    var expUpItem = new ExpUpItem()
                        .InitSkillItem(skillItem)
                        .WithUpgradeAction(item =>
                        {
                            configModel.LevelUp(skillItem);
                            expUpEvent.Trigger(skillItem);
                        });
                    expUpItems.Add(expUpItem);
                }
                tempList.RemoveAt(index); // 移除已选元素
            }
        }

        /// <summary>
        /// 从对应能力类型中随机获取一个能力
        /// </summary>
        /// <param name="skillType"></param>
        /// <returns></returns>
        private SkillItem RandomSkillItem(SkillType skillType)
        {
            var curSkillLevel = 0;
            switch (skillType)
            {
                case SkillType.Attack:
                    curSkillLevel = AppStart.attackSkillLv.Value;
                    break;
                case SkillType.AttackCD:
                    curSkillLevel = AppStart.attackCdSkillLv.Value;
                    break;
                case SkillType.Speed:
                    curSkillLevel = AppStart.speedSkillLv.Value;
                    break;
                case  SkillType.Axe:
                    curSkillLevel = Player.Default.SimpleAxe.Level;
                    break;
                case SkillType.Knife:
                    curSkillLevel = Player.Default.SimpleKnife.Level;
                    break;
                case SkillType.Shield:
                    curSkillLevel = Player.Default.SimpleCircle.Level;
                    break;
                case SkillType.Sword:
                    curSkillLevel = Player.Default.SimpleSword.Level;
                    break;
                case SkillType.Ball:
                    curSkillLevel = Player.Default.SimpleBall.Level;
                    break;
            }

            //初始等级默认为0
            return configModel.GetSkillItemConfig(curSkillLevel+1, skillType);
        }
    }
    
}
